package com.managedBean.cart;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.primefaces.event.ToggleEvent;

import com.dto.Item;
import com.dto.cart.CartDTO;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;

@ManagedBean(name = "cart")
@SessionScoped
public class CartManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7038334022444252013L;
	private transient List<CartDTO> cartItemsList;
	private CartDTO selectedCartDTO;
	private double subTotal;
	private double total;
	private double shippingCharges;
	private Date dateOfOrder;
	
	public CartManagedBean() {
		cartItemsList = new ArrayList<CartDTO>();
		shippingCharges = 20;
		dateOfOrder=new Date();
	}

	public void toggle(ToggleEvent event) {

	}

	public void addToCart(Item item) {
		CartDTO cartDTO = new CartDTO();
		cartDTO.setItemName(item.getName());
		cartDTO.setQty(item.getQty() + "");
		cartDTO.setPrice(item.getSrp());
		cartDTO.setSize(item.getSize());
		cartDTO.setBrand(item.getBrand());
		cartDTO.setItemId(item.getMoreSizeItemId());
		boolean found = false;
		for (CartDTO var : cartItemsList) {
			if (var.getItemId() == item.getMoreSizeItemId()) {
				var.setQty(Integer.parseInt(var.getQty()) + item.getQty() + "");
				found = true;
				break;
			}
		}
		cartDTO.setsNo(cartItemsList.size()+1);
		if (!found) {
			cartItemsList.add(cartDTO);
		}
		subTotal = subTotal + (item.getSrp() * item.getQty());
		total = subTotal + shippingCharges;
	}

	public void removeFromCart() {
		cartItemsList.remove(selectedCartDTO);
		subTotal = subTotal - (selectedCartDTO.getPrice() * Integer.parseInt(selectedCartDTO.getQty()));
		total = subTotal + shippingCharges;
		selectedCartDTO = null;
		for(int i=0;i<cartItemsList.size();i++){
			cartItemsList.get(i).setsNo(i+1);
		}
	}

	public String proceedToCheckOut() {
		return "checkout";
	}

	public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {
		Document pdf = (Document) document;
        pdf.open();
        pdf.setPageSize(PageSize.A4);

		ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
		String logo = servletContext.getRealPath("") + File.separator + "images" + File.separator + "prime_logo.png";
		
		pdf.add(Image.getInstance(logo));
	}

	/**
	 * @return the cartItemsList
	 */
	public List<CartDTO> getCartItemsList() {
		return cartItemsList;
	}

	/**
	 * @param cartItemsList
	 *            the cartItemsList to set
	 */
	public void setCartItemsList(List<CartDTO> cartItemsList) {
		this.cartItemsList = cartItemsList;
	}

	/**
	 * @return the selectedCartDTO
	 */
	public CartDTO getSelectedCartDTO() {
		return selectedCartDTO;
	}

	/**
	 * @param selectedCartDTO
	 *            the selectedCartDTO to set
	 */
	public void setSelectedCartDTO(CartDTO selectedCartDTO) {
		this.selectedCartDTO = selectedCartDTO;
	}

	/**
	 * @return the subTotal
	 */
	public double getSubTotal() {
		return subTotal;
	}

	/**
	 * @param subTotal
	 *            the subTotal to set
	 */
	public void setSubTotal(double subTotal) {
		this.subTotal = subTotal;
	}

	/**
	 * @return the total
	 */
	public double getTotal() {
		return total;
	}

	/**
	 * @param total
	 *            the total to set
	 */
	public void setTotal(double total) {
		this.total = total;
	}

	/**
	 * @return the shippingCharges
	 */
	public double getShippingCharges() {
		return shippingCharges;
	}

	/**
	 * @param shippingCharges
	 *            the shippingCharges to set
	 */
	public void setShippingCharges(double shippingCharges) {
		this.shippingCharges = shippingCharges;
	}

	/**
	 * @return the dateOfOrder
	 */
	public Date getDateOfOrder() {
		return dateOfOrder;
	}

	/**
	 * @param dateOfOrder the dateOfOrder to set
	 */
	public void setDateOfOrder(Date dateOfOrder) {
		this.dateOfOrder = dateOfOrder;
	}

}
